package com.j2eefast.flowable.bpm.controller;

import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.io.file.MimeType;
import com.j2eefast.common.core.utils.FileUploadUtil;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.flowable.bpm.entity.BpmBidQualitySafetyEntity;
import com.j2eefast.flowable.bpm.entity.BpmProcessDefinitionEntity;
import com.j2eefast.flowable.bpm.entity.BpmTaskFromEntity;
import com.j2eefast.flowable.bpm.service.*;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.sys.service.SysUserService;
import com.j2eefast.framework.utils.Global;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 测绘公司项目质量安全监管表
 * @author summer
 * @date 2021-06-4 22:19
 */
@Controller
@RequestMapping("bpm/bidqualitysafety")
public class BpmBidQualitySafetyController extends BaseController{

    private String prefix = "modules/bpm/bidqualitysafety";

    private String typeName = "";
    private String pathName="";
    private Long fileSize=0L;

    private String typeName2 = "";
    private String pathName2="";
    private Long fileSize2=0L;


    /**
     * 必须注入实例关联表单服务
     */
    @Autowired
    private BpmTaskFromService bpmTaskFromService;
    @Autowired
    private BpmBidQualitySafetyService bpmBidQualitySafetyService;
    @Autowired
    private ProcdefService procdefService;
    @Autowired
    private FlowableCommentService flowableCommentService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 定义关联表单申请表单URL对应此处
     */
    @GetMapping("/add/{id}")
    @RequiresPermissions("bpm:bidqualitysafety:add")
    public String add(@PathVariable("id") Long id, ModelMap mmp){
        //通过页面传入的表单ID查询表单关联信息
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getTaskFromById(id);
        mmp.put("bpmTaskFrom", bpmTaskFrom);
        BpmProcessDefinitionEntity proDef = procdefService.getById(bpmTaskFrom.getProcessDefinitionKey());
        mmp.put("proDef", proDef);
        mmp.put("user", UserUtils.getUserInfo());
        return prefix + "/add";
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @BussinessLog(title = "相关文件盖章申请表", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @RequiresPermissions("bpm:bidqualitysafety:add")
    @ResponseBody
    public ResponseData addForm(@Validated BpmBidQualitySafetyEntity bpmBidQualitySafetyEntity){
        //校验参数
        ValidatorUtil.validateEntity(bpmBidQualitySafetyEntity);
        bpmBidQualitySafetyEntity.setPhotoUrl(pathName);
        bpmBidQualitySafetyEntity.setProjectInfoUrl(pathName2);
        return bpmBidQualitySafetyService.addForm(bpmBidQualitySafetyEntity);
    }

    /**
     * 新增并悬挂
     */
    @RepeatSubmit
    @BussinessLog(title = "悬挂开标信息登记表", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/saveAsDraft", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("bpm:bidqualitysafety:add")
    public ResponseData saveAsDraft(@Validated BpmBidQualitySafetyEntity bpmBidQualitySafetyEntity){
        bpmBidQualitySafetyEntity.setCreateBy(bpmBidQualitySafetyEntity.getLaunchName());
        bpmBidQualitySafetyEntity.setPhotoUrl(pathName);
        bpmBidQualitySafetyEntity.setPhotoUrl(pathName2);
        return bpmBidQualitySafetyService.saveAsDraft(bpmBidQualitySafetyEntity);
    }

    /**
     * 表单详情
     * @param taskId
     * @param businessKey
     * @param mmap
     * @return
     */
    @GetMapping("/view")
    @RequiresPermissions("bpm:bidqualitysafety:view")
    public String view(@RequestParam(value="taskId", required=true)String taskId,
                       @RequestParam(value="businessKey", required=true)String businessKey,
                       ModelMap mmap){
        BpmBidQualitySafetyEntity bpmBidQualitySafetyEntity = bpmBidQualitySafetyService.getFormById(businessKey);
        mmap.put("bpmForm", bpmBidQualitySafetyEntity);
        mmap.put("taskId", taskId);
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getById(bpmBidQualitySafetyEntity.getFormId());
        mmap.put("bpmTaskFrom", bpmTaskFrom);
        BpmProcessDefinitionEntity proDef = procdefService.getById(bpmTaskFrom.getProcessDefinitionKey());
        mmap.put("proDef", proDef);
        mmap.put("comment", flowableCommentService.getFlowCommentVosByProcessInstanceId(bpmBidQualitySafetyEntity.getProcessInstanceId()));
        return prefix + "/view";
    }


    @RepeatSubmit
    @BussinessLog(title = "相关文件盖章申请表", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("bpm:bidqualitysafety:add")
    public ResponseData sendForm(@RequestParam(value="formDraftId", required=true)String formDraftId){
        return bpmBidQualitySafetyService.sendForm(formDraftId);
    }



    /**
     * 修改
     */
    @GetMapping("/edit")
    @RequiresPermissions("bpm:bidqualitysafety:add")
    public String edit(@RequestParam(value="taskId", required=true)String taskId,
                       @RequestParam(value="businessKey", required=true)String businessKey,
                       ModelMap mmap){
        BpmBidQualitySafetyEntity bpmOaForm = bpmBidQualitySafetyService.getFormById(businessKey);
        mmap.put("bpmForm", bpmOaForm);
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getById(bpmOaForm.getFormId());
        mmap.put("bpmTaskFrom", bpmTaskFrom);
        BpmProcessDefinitionEntity proDef = procdefService.getById(bpmTaskFrom.getProcessDefinitionKey());
        mmap.put("proDef", proDef);
        mmap.put("taskId", taskId);
        return prefix + "/edit";
    }

    /**
     * 修改待发流程表单
     */
    @RepeatSubmit
    @RequiresPermissions("bpm:bidqualitysafety:add")
    @BussinessLog(title = "编辑开标信息登记表", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editForm(BpmBidQualitySafetyEntity bpmBidQualitySafetyEntity){
        ValidatorUtil.validateEntity(bpmBidQualitySafetyEntity);
        bpmBidQualitySafetyEntity.setPhotoUrl(pathName);
        bpmBidQualitySafetyEntity.setPhotoUrl(pathName2);
        return bpmBidQualitySafetyService.editForm(bpmBidQualitySafetyEntity)? success(): error("修改失败!");
    }

    @GetMapping("/flow")
    @RequiresPermissions("bpm:bidqualitysafety:add")
    public String flow(@RequestParam(value="taskId", required=true)String taskId,
                       @RequestParam(value="businessKey", required=true)String businessKey,
                       ModelMap mmap){

        BpmBidQualitySafetyEntity bpmOaForm = bpmBidQualitySafetyService.getFormById(businessKey);
        mmap.put("bpmForm", bpmOaForm);
        mmap.put("taskId", taskId);
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getById(bpmOaForm.getFormId());
        mmap.put("bpmTaskFrom", bpmTaskFrom);
        BpmProcessDefinitionEntity proDef = procdefService.getById(bpmTaskFrom.getProcessDefinitionKey());
        mmap.put("proDef", proDef);
        mmap.put("comment", flowableCommentService.getFlowCommentVosByProcessInstanceId(bpmOaForm.getProcessInstanceId()));
        return prefix + "/flow";
    }


    /**
     * 上传资料照片
     */
    @RepeatSubmit
    @BussinessLog(title = "上传资料", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/doUploadFile", method = RequestMethod.POST)
    @RequiresPermissions("bpm:bidqualitysafety:doUploadFile")
    @ResponseBody
    public Map<String, Object> doUploadFile(MultipartFile file) {
        Map<String, Object> map=new HashMap<>();
        try {
            if (!file.isEmpty()) {
                pathName = FileUploadUtil.uploadWeb(Global.getTempPath(), file);
                typeName = pathName.substring(pathName.indexOf(".")+1);
                fileSize=file.getSize();
                if (!Arrays.asList(MimeType.IMAGE_EXTENSION).contains(typeName)) {
                    map.put("status",0);
                    return map;
                }
                map.put("status",1);
                map.put("url",pathName);
                return map;
            } else {
                map.put("status",0);
                return map;
            }
        } catch (IOException e) {
            map.put("status",0);
            return map;
        }
    }

    /**
     * 上传工程信息概况表
     */
    @RepeatSubmit
    @BussinessLog(title = "上传资料", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/doUploadFile2", method = RequestMethod.POST)
    @RequiresPermissions("bpm:bidqualitysafety:doUploadFile2")
    @ResponseBody
    public Map<String, Object> doUploadFile2(MultipartFile file) {
        Map<String, Object> map=new HashMap<>();
        try {
            if (!file.isEmpty()) {
                pathName2 = FileUploadUtil.uploadWeb(Global.getTempPath(), file);
                typeName2 = pathName2.substring(pathName2.indexOf(".")+1);
                fileSize2=file.getSize();
                if (!Arrays.asList(MimeType.WORD_EXTENSION).contains(typeName2)) {
                    map.put("status",0);
                    return map;
                }
                map.put("status",1);
                map.put("url",pathName2);
                return map;
            } else {
                map.put("status",0);
                return map;
            }
        } catch (IOException e) {
            map.put("status",0);
            return map;
        }
    }


}